package statistics;

import java.util.Random;


public class NormalDistribution extends Distribution{

    protected double mu;
    protected double sigma;
    
    public NormalDistribution( double mu, double sigma, Random random ){
        this.mu = mu;
        this.sigma = sigma;
 
    }
            

    public double expectation() {
       return mu;
    }


    public double variance() {
        return sigma*sigma;
    }


    public double nextRandom() {
        double y=0;
        double u;
        double x;
        boolean accept = false;
        Random rand = new Random();
        Distribution exp = new ExponentialDistribution(1, rand);
        
        while(y==0){
            y = exp.nextRandom();
            u = random.nextDouble();
            if(u>Math.exp(-(y-1)*(y-1)/2.0))
                y=0;
        }
        u = random.nextDouble();
        if(u<=0.5) {
            x = y;
        }
        else {
            x= -y;
        }
        return x;
               
    }
        
}
    
    